package com.bkgy.jobs;

import com.bkgy.controller.planking.service.IProfit1870JobService;
import com.bkgy.controller.tcmReport.service.IRepTcmOutputPlanActService;
import com.bkgy.controller.technology.mapper.ICalProcessMapper;
import com.bkgy.controller.technology.mapper.ICgl1870ProcessMapper;
import com.bkgy.controller.technology.mapper.ITcmProcessMapper;
import com.bkgy.controller.technology.service.ICalProcessService;
import com.bkgy.controller.technology.service.ICgl1870ProcessService;
import com.bkgy.controller.technology.service.ITcm2280ProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 1.镀锌生产效益分析，结果存入表 tb_profit1870
 * 2.镀锌工艺符合度分析，结果存入表 cgl1870_process_datas
 * 3.连退工艺符合度分析，结果存入表 cal_process_datas
 * 4.酸轧工艺符合度分析，结果存入表 tcm2280_process_datas
 * 5.抽取镀锌外板信息做报表记录
 * 6.更新日计划、下达、实际报表基础数据 REP_TCM_OUTPUT_PLAN_ACT
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class Profit1870RollJob {
    private final ITcm2280ProcessService tcmService;
    private final ITcmProcessMapper tcmMapper;
    private final ICalProcessService calService;
    private final ICalProcessMapper calMapper;
    private final IProfit1870JobService profitExcService;
    private final ICgl1870ProcessService cgl1870Service;
    private final ICgl1870ProcessMapper cgl1870Mapper;
    private final IRepTcmOutputPlanActService repTcmService;

    public void exec() {
        try {
            //任务1：镀锌生产效益分析
            profitExcService.profit1870Job();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        //任务2:镀锌工艺符合度分析
        try {
            String beginTime = cgl1870Mapper.selectTopOneProdEndTime();

            //补偿因MongoDB曲线数据延迟导致缺少验证问题
            cgl1870Service.compensateCheck();

            cgl1870Service.transData(beginTime);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        //任务3：连退工艺符合度分析
        try {
            String beginTime = calMapper.selectTopOneProdEndTime();
            calService.transData(beginTime);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        //任务4：酸轧工艺符合度分析
        try {
            String beginTime = tcmMapper.selectTopOneProdEndTime();
            String endTime = "99991231235959";
            tcmService.transData(beginTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        //任务5：提取外板报表信息
        try {
            //补充外板表缺陷
            cgl1870Service.updatePlankDefectInfo();

            cgl1870Service.splitPlankService();
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }

        //任务6：初始化计划产量、下达产量
        try {
            repTcmService.initDayPlanXiadaJob(new Date());
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }
}
